package com.xing.controller;

import com.xing.entity.User;
import com.xing.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserHandler {

    @Autowired
    private UserRepository userRepository;

    //严格遵守rest风格
    // 查 get
    // 增加 post
    // 修改 put
    // 删除 delete

    @GetMapping("/findAll")
    public List<User> findAll(){
        return  userRepository.findAll();
    }
   //@PathVariable是用来,对指定请求的URL路径里面的变量

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") long id){
        return userRepository.findById(id);
    }

    //@RequestBody把前端传过来的json对象，转成Java
    @PostMapping("/save")
    public void save(@RequestBody User user){
        userRepository.save(user);
    }

    @PutMapping("/update")
    public void update(@RequestBody User user){
        userRepository.update(user);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        userRepository.deleteById(id);
    }

}
